package com.module.ble.ringMonitorService

import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.activity.ComponentActivity
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2025/4/14
 *
 * 描述：
 *
 * 修订历史：
 *
 */
// 统一服务控制器
object RingCusServiceController {

    // 当前活跃的服务集合
    private val activeServices = mutableSetOf<ServiceType>()

    // 服务类型接口
    interface ServiceInfo {
        val serviceClass: Class<out BaseMonitorService>
        val shouldRunInBackground: Boolean
    }

    // 定义支持的服务类型
    enum class ServiceType(override val serviceClass: Class<out BaseMonitorService>,
                           override val shouldRunInBackground: Boolean = true) : ServiceInfo {
        SPORT(SportMonitorService::class.java, true),
//        SLEEP(SleepMonitorService::class.java, true),
    }

    // 启动指定类型的监控服务
    fun start(context: Context, serviceType: ServiceType) {

        // 避免重复启动同一服务
        if (isServiceRunning(serviceType)) {
            return
        }

        val serviceIntent = Intent(context, serviceType.serviceClass)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(serviceIntent)
        } else {
            context.startService(serviceIntent)
        }

        activeServices.add(serviceType)
    }

    // 停止指定类型的服务
    fun stop(context: Context, serviceType: ServiceType) {
        val serviceIntent = Intent(context, serviceType.serviceClass)
        context.stopService(serviceIntent)
        activeServices.remove(serviceType)
    }

    // 停止所有服务
    fun stopAll(context: Context) {
        // 创建临时列表避免并发修改异常
        val services = activeServices.toList()
        services.forEach { stop(context, it) }
    }

    // 绑定生命周期到Activity - 支持多服务
    fun bindToLifecycle(activity: ComponentActivity, serviceTypes: Set<ServiceType>) {
        activity.lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onStart(owner: LifecycleOwner) {
                serviceTypes.forEach { serviceType ->
                    start(activity.applicationContext, serviceType)
                }
            }

            override fun onStop(owner: LifecycleOwner) {
                serviceTypes.forEach { serviceType ->
                    if (serviceType.shouldRunInBackground) {
                        // 保持服务运行
                    } else {
                        stop(activity, serviceType)
                    }
                }
            }
        })
    }

    // 检查服务是否正在运行
    fun isServiceRunning(serviceType: ServiceType): Boolean {
        return activeServices.contains(serviceType)
    }

    // 检查并恢复应该运行的服务
    fun checkAndRestoreServices(context: Context) {
        ServiceType.values().forEach { serviceType ->
            if (serviceType.shouldRunInBackground && !isServiceRunning(serviceType)) {
                // 服务应该运行但实际没运行，尝试恢复
                start(context, serviceType)
            }
        }
    }
}